package com.xk._01基础篇._10映射Map;

import com.xk._01基础篇._10映射Map.file.FileInfo;
import com.xk._01基础篇._10映射Map.file.Files;
import com.xk._01基础篇._10映射Map.map.Map;
import com.xk._01基础篇._10映射Map.map.TreeMap;
import com.xk._01基础篇._10映射Map.set.Set;
import com.xk._01基础篇._10映射Map.set.TreeSet;
import com.xk.tools.Times;

/**
 * @description:
 * @author: xu
 * @date: 2022/9/27 8:16
 */
public class Main {
    private static final String PATH = "X:\\NewCode\\00JavaSe\\DataStructure\\dataStruction-00基础篇\\dataStruction";
    private static final String PATH1 = "X:\\newJava\\JDK8\\src";

    public static void main(String[] args) {
        test02();
    }

    private static void test01() {
        FileInfo fileInfo = Files.read(PATH, new String[]{"java"});
        System.out.println("文件数量：" + fileInfo.getFiles());
        System.out.println("代码行数：" + fileInfo.getLines());
        String[] words = fileInfo.words();
        System.out.println("单词数量：" + words.length);

        Times.test("test TreeSet", new Times.Task() {
            @Override
            public void execute() {
                testMap(new TreeMap<String, Integer>(), words);
            }
        });

        Times.test("test TreeSet", new Times.Task() {
            @Override
            public void execute() {
                testMap(new TreeMap<String, Integer>(), words);
            }
        });
    }

    private static void testMap(TreeMap<String, Integer> map, String[] words) {
        for (String word : words) {
            Integer count = map.get(word);
            count = (count == null) ? 1 : count + 1;
            map.put(word, count);
        }

        map.traversal(new Map.Visitor<String, Integer>() {
            @Override
            public boolean visit(String key, Integer value) {
                System.out.println(key + "：" + value);
                return false;
            }
        });
    }

    private static void test02() {
        FileInfo fileInfo = Files.read(PATH1, new String[]{"java"});
        System.out.println("文件数量：" + fileInfo.getFiles());
        System.out.println("代码行数：" + fileInfo.getLines());
        String[] words = fileInfo.words();
        System.out.println("单词数量：" + words.length);

        Times.test("test TreeSet", new Times.Task() {
            @Override
            public void execute() {
                testSet(new TreeSet<String>(), words);
            }
        });
    }

    private static void testSet(Set<String> set, String[] words) {
        for (String word : words) {
            set.add(word);
        }
        for (String word : words) {
            set.contains(word);
        }
        for (String word : words) {
            set.remove(word);
        }
    }
}
